﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Domain.Abstract;
using Domain.Entity;

namespace Domain.Concrete
{
    public class EFPictureRepository : IPictureRepository
    {
        public EFDbContext Context { get; set; }

        public EFPictureRepository()
        {
            Context = EFDbContext.Instance;
        }

        public EFPictureRepository(EFDbContext context)
        {
            Context = context;
        }

        public IQueryable<Picture> Pictures
        {
            get { return Context.Pictures; }
        }

        public void Save(Picture picture)
        {
            if (picture.Id == 0)
                Context.Pictures.Add(picture);
            else
                Context.Entry(picture).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public Picture Find(int id)
        {
            return Context.Pictures.FirstOrDefault(x => x.Id == id);
        }

        public void Delete(Picture picture)
        {
            var dbPicture = Pictures.SingleOrDefault(x => x.Id == picture.Id);

            if (dbPicture != null)
                Context.Pictures.Remove(dbPicture);
            Context.SaveChanges();
        }
    }
}
